package zzk.gwxer.tttttt.hanlan.ding.ui.component

import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun NumberPicker(
    value: Int,
    onValueChange: (Int) -> Unit,
    modifier: Modifier = Modifier,
    minValue: Int = 0,
    maxValue: Int = 10,
    step: Int = 1,
    colors: NumberPickerColors = NumberPickerDefaults.colors()
) {
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center
    ) {
        // 减少按钮
        PickerButton(
            enabled = value > minValue,
            onClick = { onValueChange((value - step).coerceAtLeast(minValue)) },
            icon = Icons.Default.KeyboardArrowDown,
            colors = colors
        )

        // 数值显示（带动画）
        Text(
            text = value.toString(),
            modifier = Modifier
                .width(60.dp)
                .animateContentSize(),
            fontSize = 20.sp,
            color = colors.textColor,
            textAlign = TextAlign.Center
        )

        // 增加按钮
        PickerButton(
            enabled = value < maxValue,
            onClick = { onValueChange((value + step).coerceAtMost(maxValue)) },
            icon = Icons.Default.KeyboardArrowUp,
            colors = colors
        )
    }
}

@Composable
private fun PickerButton(
    enabled: Boolean,
    onClick: () -> Unit,
    icon: ImageVector,
    colors: NumberPickerColors
) {
    val buttonSize = 40.dp
    val animatedSize by animateDpAsState(
        targetValue = if (enabled) buttonSize else buttonSize * 0.8f,
        label = "buttonSize"
    )

    IconButton(
        onClick = {
            if (enabled) onClick()
        },
        modifier = Modifier.size(animatedSize),
        enabled = enabled
    ) {
        Icon(
            imageVector = icon,
            contentDescription = null,
            tint = if (enabled) colors.enabledColor else colors.disabledColor,
            modifier = Modifier.size(24.dp)
        )
    }
}

// 颜色配置
data class NumberPickerColors(
    val enabledColor: Color,
    val disabledColor: Color,
    val textColor: Color
)

object NumberPickerDefaults {
    @Composable
    fun colors(
        enabledColor: Color = MaterialTheme.colorScheme.primary,
        disabledColor: Color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.3f),
        textColor: Color = MaterialTheme.colorScheme.onSurface
    ) = NumberPickerColors(
        enabledColor = enabledColor,
        disabledColor = disabledColor,
        textColor = textColor
    )
}